<?php 
// _fpdf_mc_table.php
class PDF extends FPDF
{
	var $widths;
	var $aligns;

	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}
	
	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}
	
	function Row($data, $fill = true, $need_cell_border=false)
	{
		//$fill = false;
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('');
		
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		
		$cell_height = 6;
		$h=$cell_height*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			//$this->Rect($x,$y,$w,$h);
			//Print the text
			$border = ''; 
			$this->MultiCell($w,$cell_height,$data[$i], $border, $a, $fill);
			if($need_cell_border){ 
				if ($i == 4 || $i == 9|| $i == 14|| $i == 19|| $i == 24|| $i == 29|| $i == 34) 
					$this->Line($x, $y, $x, $y+$h);
			} 			
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}
	
	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}
	
	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
	function setHeaderTitle($title){
		$this->setHeaderTitle($title);
	}
	//Page header
	function Header()
	{
		global $header, $w, $title;
		//Logo
	    //$this->Image('./images/logo-irou.jpg',10,8,33);
	    $this->Image('./images/logo-irou.jpg',20,8,15);
	    //Arial bold 15
	    $this->SetFont('Arial','B',13);
	    //Move to the right
	    //$this->Cell(80);
	    //Title
	    $this->Cell(0,10,$title,0,0,'C');
	    //Line break
	    $this->Ln(15);
	    
	    // If table is needed 
	    if($header !=''){
		    //Colors, line width and bold font
		    $this->SetFillColor(255,0,0);
		    $this->SetTextColor(255);
		    $this->SetDrawColor(128,0,0);
		    $this->SetLineWidth(.3);
		    $this->SetFont('Arial','B',8);
		    //Header
	        //$this->Cell(10,10,'Bil.',1,0,'C',true);
		    for($i=0;$i<count($header);$i++)
		        $this->Cell($w[$i],7,preg_replace("/_/"," ",$header[$i]),1,0,'C',true);
		    $this->Ln();
	    }
	}
	
	//Page footer
	function Footer()
	{
	    //Position at 1.5 cm from bottom
	    $this->SetY(-15);
	    //Arial italic 8
	    $this->SetFont('Arial','I',8);
	    //Page number
	    $this->Cell(0,10,'Cetakan pada '.date("d/m/Y H:i:s").'    Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
	
	//Colored table
	function FancyTable($header,$data)
	{
	    global $w;
		//Colors, line width and bold font
	    $this->SetFillColor(224,235,255);
	    $this->SetTextColor(0);
	    $this->SetFont('');
	    //Data
	    $fill=false;
	    $x=0;
	    foreach($data as $row)
	    {
	        if($row[$x]=='') $row[$x]='--nil--';
			//$this->Cell(10,7,$x,1,0,'C',true);
			$this->Cell($w[$x],10,ucwords($row[$x]),'LR',0,'L',$fill);
	        $x++;
	        if($x == sizeof($row)){
	        	$x=0;
		        $this->Ln();
		        $fill=!$fill;
	        }
	    }
	    //$this->Ln();
	    //$this->Cell(array_sum($w),0,'','B');
	    $this->Cell(0,0,'','T');
	}	
}
